package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);

        dishService.saveWithFlavor(dishDTO);//后绪步骤开发

        //获取精确受到影响的那份缓存数据 需要设置key
        //dish_ +分类id
        String dishkey = "dish_"+dishDTO.getCategoryId();
        redisTemplate.delete(dishkey);

        return Result.success();
    }

    //分页查询
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){

        log.info("分页查询。{}",dishPageQueryDTO);
        PageResult pageResult=  dishService.page(dishPageQueryDTO);

        return Result.success(pageResult);
    }

    //先查后该
    @GetMapping("/{id}")
    @ApiOperation("修改菜品")
    public Result<DishVO> selectId(@PathVariable Long id ){
        log.info("根据id查询，id{}" ,id);

        DishVO dishVO =  dishService.selectId(id);


        return Result.success(dishVO);
    }

    //修改
    @PutMapping
    @ApiOperation("编辑菜品")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品，{}",dishDTO);
        dishService.update(dishDTO);

        //批量删除后 可能会影响多张表  简单点把所有的带有dish_的全删 菜品缓存全部清理
        clean("dish_*");

        return Result.success();
    }

    //删除菜品
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result delete(@RequestParam List<Long> ids){
        log.info("删除菜品,{}",ids);
        dishService.delete(ids);
        //批量删除后 可能会影响多张表  简单点把所有的带有dish_的全删 菜品缓存全部清理
        clean("dish_*");

        return Result.success();
    }

    //根据分类id 查询 菜品
    @GetMapping("/list")
    @ApiOperation("查询菜品")
    public Result selectCategory(Long categoryId){
        List<Dish>  dishList = dishService.categoryId(categoryId);
        return Result.success(dishList);
    }
    //菜品的起售和停售
    @PostMapping("/status/{status}")
    @ApiOperation("起售和停售")
    public Result startOr(@PathVariable Integer status,Long id){
        log.info("菜品状态和id,{},{}",status,id);
        dishService.startOr(status,id);
        //批量删除后 可能会影响多张表  简单点把所有的带有dish_的全删 菜品缓存全部清理
        clean("dish_*");
        return Result.success();
    }

    //抽取 清理缓存的操作

    private void clean(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }


}
